﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using ToolUtil;
using zyconfig;

namespace zymain
{
    public partial class FrmMain : Form
    {
        private string _uid;
        private int gjm;
        public FrmMain()
        {
            InitializeComponent();
        }

        private void uname_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar == '\r')
            {
                string str = this.uname.Text;
                if (string.IsNullOrEmpty(str))
                {
                    ShowMessageBox.ShowMessageBoxTimeout("账号不能为空", "错误提示", MessageBoxButtons.OK, 2000);
                    this.uname.Focus();
                }
                else
                    this.upass.Focus();
            }
        }

        private void dlBtn_Click(object sender, EventArgs e)
        {
            string str = this.uname.Text.Trim();
            if (string.IsNullOrEmpty(str))
            {
                ShowMessageBox.ShowMessageBoxTimeout("账号不能为空", "错误提示", MessageBoxButtons.OK, 2000);
                this.uname.Focus();
            }
            else
            {
                using (DbOperHandler _open = new Common().Doh())
                {
                    MD5.Encrypt("ccbuptymym");
                    _open.Reset();
                    _open.ConditionExpress = " 用户代码=" + str;
                    string s = _open.GetField("系统用户信息", "密码").ToString();
                    if (this.upass.Text.Trim().Equals(MD5.decrypt(s)))
                    {
                        this._uid = str;
                        this.gjm=Convert.ToInt32(_open.GetField("系统用户信息", "馆键码").ToString().Trim());
                        string glqx = _open.GetField("系统用户信息", "管理权限").ToString().Trim();
                        int byteLength = glqx.Length / 10;
                        string[] strArrayH = new string[byteLength];
                        //将字符串分割为长度为4的字符数组
                        for (int i = 0; i < (byteLength); i++)
                        {
                            try
                            {
                                strArrayH[i] = glqx.Substring(10 * i, 10);//i-起始位置，4-子串长度
                                //Console.WriteLine(i+" : "+strArrayH[i]);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.ToString(), "异常提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                continue;
                            }
                        }
                        this.timer1_Tick(sender, e);
                    }
                    else
                    {
                        ShowMessageBox.ShowMessageBoxTimeout("账号或密码错误", "错误提示", MessageBoxButtons.OK, 2000);
                        this.uname.Focus();
                    }
                }
            }
            this.timer1.Enabled = false;
        }

        private void FrmMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)//e.Control是判断用户是否按下ctrl键 
            {
                Process.GetCurrentProcess().Kill();
                //Application.Exit();
            }
        }

        private void upass_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                string str = this.upass.Text;
                if (string.IsNullOrEmpty(str))
                {
                    ShowMessageBox.ShowMessageBoxTimeout("密码不能为空", "错误提示", MessageBoxButtons.OK, 2000);
                    this.upass.Focus();
                }
                else
                    this.dlBtn_Click(sender, e);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            FrmTool f = new FrmTool();
            f.UserID = this._uid;
            f.Gjm = this.gjm;
            f.Show();
            base.Hide();
            this.timer1.Enabled = false;
        }
    }
}
